
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bullacows;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import youhuizi.StatelessBeanRemote;
import youhuizi.timerBeanRemote;

/**
 *
 * @author huizi.you
 */
public class Main {
    @Resource(name = "jms/postbox")
    private static Queue postbox;
    @Resource(name = "jms/postboxFactory")
    private static ConnectionFactory postboxFactory;
    @EJB
    private static timerBeanRemote timerBean;
    @EJB
    private static StatelessBeanRemote statelessBean;

    /**
     * @param args the command line arguments
     */
    public  static void main(String[] args) throws JMSException {

       
        System.out.println(""+statelessBean.generateString());

        System.out.println(""+statelessBean.generateString());
        int NUM_MSGS = 10;
        
      String s = "This is message";

        for (int i = 0; i < NUM_MSGS; i++) {
     s = "This is message " + (i + 1);
          //  sendJMSMessageToPostbox(s);


    }
    }

    private Message createJMSMessageForjmsPostbox(Session session, Object messageData) throws JMSException {
        // TODO create and populate message to send
        TextMessage tm = session.createTextMessage();
        tm.setText(messageData.toString());
        return tm;
    }

    private void sendJMSMessageToPostbox(Object messageData) throws JMSException {
        Connection connection = null;
        Session session = null;
        try {
            connection = postboxFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(postbox);
            messageProducer.send(createJMSMessageForjmsPostbox(session, messageData));
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
